{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 对y=x+b求偏导数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[6., 6., 6., 6., 6.],\n",
       "        [6., 6., 6., 6., 6.]])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.ones((2,5),requires_grad=True)\n",
    "b = 6\n",
    "y = 6*x+b\n",
    "holder_weights = torch.ones((2,5))\n",
    "y.backward(holder_weights)\n",
    "x.grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[6., 6., 6., 6., 6.],\n",
       "        [6., 6., 6., 6., 6.]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.ones((2,5),requires_grad=True)\n",
    "b = 6\n",
    "y = 6*x+b\n",
    "holder_weights = torch.ones((2,5))\n",
    "y.backward(holder_weights)\n",
    "x.grad"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 对sinx求导数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.5403, 0.5403, 0.5403, 0.5403]])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.ones((1,4),requires_grad=True)\n",
    "y = torch.sin(x)\n",
    "holder_weights = torch.ones((1,4))\n",
    "y.backward(holder_weights)\n",
    "x.grad"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 对指数函数y=e^x求导数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 2.7183, 20.0855],\n",
       "        [ 7.3891, 54.5981]])"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "x = torch.Tensor([[1,3],[2,4]])\n",
    "x.requires_grad=True\n",
    "y = torch.pow(math.e,x)\n",
    "holder_weights = torch.ones_like(x)\n",
    "y.backward(holder_weights)\n",
    "x.grad"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### \t对幂函数y=x^e求导"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 2.7183, 17.9524],\n",
       "        [ 8.9444, 29.4309]])"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "x = torch.Tensor([[1,3],[2,4]])\n",
    "x.requires_grad=True\n",
    "y = torch.pow(x,math.e)\n",
    "holder_weights = torch.ones_like(x)\n",
    "y.backward(holder_weights)\n",
    "x.grad"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 对数函数y=lnx求导数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1.0000, 0.3333],\n",
       "        [0.5000, 0.2500]])"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "x = torch.Tensor([[1,3],[2,4]])\n",
    "x.requires_grad=True\n",
    "y = torch.log(x)\n",
    "holder_weights = torch.ones_like(x)\n",
    "y.backward(holder_weights)\n",
    "x.grad"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### sigmod的导数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.1966, 0.0452],\n",
       "        [0.1050, 0.0177]])"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "x = torch.Tensor([[1,3],[2,4]])\n",
    "x.requires_grad=True\n",
    "y = 1/(1+math.e**-x)\n",
    "holder_weights = torch.ones_like(x)\n",
    "y.backward(holder_weights)\n",
    "x.grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
